package com.pd.web.controller.admin;

import com.alibaba.fastjson.JSON;
import com.pd.business.ResourceService;
import com.pd.business.RoleService;
import com.pd.constant.SysContant;
import com.pd.dao.model.Role;
import com.pd.dao.model.TreeNode;
import com.pd.domain.ResponseVo;
import com.pd.exception.BusinessException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * Created by dong peng on 2018/1/18.
 */
@Controller
@RequestMapping("/admin/role")
public class ManagerRoleCtl extends ManageBaseController {

    @Autowired
    private RoleService roleService;

    @Autowired
    private ResourceService resourceService;

    /**
     *查询角色
     */
    @RequestMapping(value = "/query")
    public @ResponseBody String query(){

        List<Role> roles = roleService.query();
        ResponseVo responseVo = new ResponseVo();
        responseVo.setTip(SysContant.SUCCESS);
        responseVo.setMsg("调用成功");
        responseVo.addRetData("roles",roles);

        return JSON.toJSONString(responseVo);
    }

    /**
     * 查询角色绑定的资源
     * @return
     */
    @RequestMapping(value = "/queryRoleResource")
    public @ResponseBody String queryRoleResource(Role role) {
        if(role == null || role.getId() == null) {
            throw  new BusinessException("没有传入正确id");
        }
        List<TreeNode> treeNodes = resourceService.queryTreeNode(role.getId());

        ResponseVo responseVo = new ResponseVo();
        responseVo.setTip(SysContant.SUCCESS);
        responseVo.setMsg("查询成功");
        responseVo.addRetData("rows",treeNodes);

        return JSON.toJSONString(responseVo);
    }

    /**
     * 新增角色
     * @param role
     * @return
     */
    @RequestMapping("/add")
    public @ResponseBody ResponseVo add(@RequestBody Role role) {
        roleService.add(role);
        ResponseVo responseVo = new ResponseVo();
        responseVo.setTip(SysContant.SUCCESS);
        return responseVo;
    }

    /**
     * 修改角色
     * @param role
     * @return
     */
    @RequestMapping("/modify")
    public @ResponseBody ResponseVo modify(@RequestBody Role role) {
        roleService.modify(role);
        ResponseVo responseVo = new ResponseVo();
        responseVo.setTip(SysContant.SUCCESS);
        return responseVo;
    }

    /**
     * 删除角色
     * @param role
     * @return
     */
    @RequestMapping("/delete")
    public @ResponseBody ResponseVo delete(@RequestBody Role role) {
        if(role == null || role.getId() == null) {
            throw  new BusinessException("没有传入正确id");
        }
        roleService.delete(role.getId());
        ResponseVo responseVo = new ResponseVo();
        responseVo.setTip(SysContant.SUCCESS);
        return responseVo;
    }


}
